<?php
/**
 * Joomla! 1.5 component sexy_polling
 *
 * @version $Id: default.php 2012-04-05 14:30:25 svn $
 * @author Simon Poghosyan
 * @package Joomla
 * @subpackage sexypolling
 * @license GNU/GPL
 *
*/

// Joomla main index.php file
define( '_JEXEC', 1 );

/*
 * This is external PHP file and used on AJAX calls, so it has not "defined('_JEXEC') or die;" part.
*/

define( 'DS', DIRECTORY_SEPARATOR );
define('JPATH_BASE', dirname(__FILE__).DS.'..'.DS.'..' );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$app =& JFactory::getApplication('site');
$app->initialise();

$module_id = JRequest::getInt('simplecontactus_module_id', 0, 'post');
$get_token = JRequest::getInt('get_token', 0, 'get');
// end

$component = JComponentHelper::getComponent( 'com_simplecontactus' );
$comparams = new JParameter( $component->params );

JResponse::allowCache( false );
JResponse::setHeader( 'Content-Type', 'text/plain' );

if($get_token == 0) {
	if ($module_id == 0) {
		if (!JRequest::checkToken()) {
			echo '[{"invalid":"invalid_token"}]';
		}
		else {
			
			$info = Array();
			
			//get from
			$fromname = $app->getCfg('fromname', $app->getCfg('sitename'));
			$mailfrom = $app->getCfg('mailfrom');
			if (!$mailfrom ) {
				$info[] = 'Mail from not set in Joomla Global Configuration';
			}
			
			//get email to
			$email_to = array();
			if ( $comparams->get('email_to') ) {
				$email_to = explode(',', $comparams->get('email_to'));
			}
			if (count($email_to) == 0) {
				$email_to = $mailfrom;
			}
			
			// Email subject
			$simplecontactus_subject= $comparams->get( 'email_subject', 'Message sent from '.$app->getCfg('sitename') );
			
			$simplecontactus_name		= strip_tags( JRequest::getVar( 'simplecontactus_name', '', 'post' ));
			$simplecontactus_email		= trim(strip_tags( JRequest::getVar( 'simplecontactus_email', '', 'post' )));
			$simplecontactus_phone		= strip_tags( JRequest::getVar( 'simplecontactus_phone', '', 'post' ));
			$simplecontactus_message 	= strip_tags( JRequest::getVar( 'simplecontactus_message', '', 'post' ));
			$simplecontactus_ip 		= strip_tags( JRequest::getVar( 'simplecontactus_ip', '', 'post' ));
			
			$mail = JFactory::getMailer();
			
			
			//generate the body
			$body = '';
			if($comparams->get('showname') == 1)
				$body .= 'Name: '.$simplecontactus_name."\r\n";
			if($comparams->get('showemail') == 1)
				$body .= 'Email: '.$simplecontactus_email."\r\n";
			if($comparams->get('showphone') == 1)
				$body .= 'Phone: '.$simplecontactus_phone."\r\n";
			if($comparams->get('showmessage') == 1)
					$body .= 'Message: '.$simplecontactus_message."\r\n";
			$body .= 'Ip: '.$simplecontactus_ip."\r\n";
			
			//Set the body
			$mail->setBody( $body );
			$info[] = 'Body set...';
			
			//Set subject
			$mail->setSubject( $simplecontactus_subject );
			$info[] = 'Subject set to: '.$simplecontactus_subject;
			
			//Set Recipient
			$mail->addRecipient( $email_to );
			$info[] = 'Recipient set: '.$email_to;
			
			//Set Sender
			$mail->setSender( array( $mailfrom, $fromname ) );
			$info[] = 'Sender set: '.$email_to;
			
			// set reply to
			if ($comparams->get( 'email_replyto')) {
				$replyto_email = $comparams->get( 'email_replyto', $mailfrom);
				$mail->ClearReplyTos();
				$mail->addReplyTo( $replyto_email, $comparams->get( 'email_replyto_name', $fromname) );
				$info[] = 'Reply to set: '.$replyto_email;
			}
			
			// add blind carbon recipient
			if ($comparams->get('email_bcc')) {
				$email_bcc = explode(',', $params->get( 'email_bcc'));
				$mail->addBCC($email_bcc);
				$info[] = 'BCC recipients added';
			}
			
			if ( $mail->Send() === true ) {
				JUtility::getToken(true);
				$info[] = 'Email sent successful';
			}
			else $info[] = 'There are problems sending email';
			
			//generates json output
			echo '[{';
			if(sizeof($info) > 0) {
				echo '"info": ';
				echo '[';
				foreach ($info as $k => $data) {
					echo '"'.$data.'"';
					if ($k != sizeof($info) - 1)
						echo ',';
				}
				echo ']';
			}
				
			echo '}]';
		}
		
	}
}
else {
	echo JUtility::getToken();
}
jexit();